package com.masterclient.app.modules.order.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.eoner.baselibrary.bean.order.ConfirmOrderData;

public class SubmitOrderCrossBorderAgreementAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    ConfirmOrderData data;

    OnCrossBorderAgreementClick onCrossBorderAgreementClick;


    public interface OnCrossBorderAgreementClick{
        void OnAgreementChanged(boolean isAgreementSelect);
    }

    public OnCrossBorderAgreementClick getOnCrossBorderAgreementClick() {
        return onCrossBorderAgreementClick;
    }

    public void setOnCrossBorderAgreementClick(OnCrossBorderAgreementClick onCrossBorderAgreementClick) {
        this.onCrossBorderAgreementClick = onCrossBorderAgreementClick;
    }

    public SubmitOrderCrossBorderAgreementAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null);
    }

    public SubmitOrderCrossBorderAgreementAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, ConfirmOrderData data) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

//        View cross_border_tip_layout = holder.itemView.findViewById(R.id.cross_border_tip_layout);
        View cross_border_agreement_layout = holder.itemView.findViewById(R.id.cross_border_agreement_layout);
        View item = holder.itemView.findViewById(R.id.item);
        View cross_border_agreement_txt = holder.itemView.findViewById(R.id.cross_border_agreement_txt);
        CheckBox cross_border_agreement_checkbox = holder.itemView.findViewById(R.id.cross_border_agreement_checkbox);


        if(data.isHas_cross_seller()){
//            cross_border_tip_layout.setVisibility(View.VISIBLE);
            cross_border_agreement_layout.setVisibility(View.VISIBLE);
            if(data.isClearance_agreement()){
                cross_border_agreement_checkbox.setChecked(true);
            }else{
                cross_border_agreement_checkbox.setChecked(false);
            }
        }else{
//            cross_border_tip_layout.setVisibility(View.GONE);
            cross_border_agreement_layout.setVisibility(View.GONE);
        }

        cross_border_agreement_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext,BridgeActivity.class);
                intent.putExtra("url",data.getInstructions().getCross_border_introduce());
                intent.putExtra("title","跨境商品用户购买须知");
                mContext.startActivity(intent);
            }
        });

        cross_border_agreement_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(onCrossBorderAgreementClick != null){
                    onCrossBorderAgreementClick.OnAgreementChanged(b);
                }
            }
        });



    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 8;
    }
}
